import regeneratorRuntime from '../utils/runtime'
import {Message} from '../utils/method'

// 用户授权
const getUserInfo = ()=>{
	return new Promise((reso,rej)=>{
		wx.getSetting({
			success: (setting)=>{
				if(setting.authSetting['scope.userInfo']){
					wx.getUserInfo({
						success:(res)=>{
							reso(res)
						}
					})
				}else{
					rej('未授权')
				}
			}
		})
	})
}

// 消息订阅
const requestSubscribeMessage = ()=>{
	return new Promise((reso,rej)=>{
		wx.requestSubscribeMessage({
			tmplIds: [...Message],
			success: (res)=> { 
				if(res.errMsg==='requestSubscribeMessage:ok'){
					let arr = Message.map(item=>{
						return res[item] === 'accept'?item:undefined
					}).filter(item=>item)
					if(arr.length===0){
						wx.showModal({
							title:"请授权当前消息订阅"
						})
					}
					reso(arr)
				}else{
					rej("授权消息订阅失败")
				}
			}
		})
	})
}

export {
	getUserInfo,
	requestSubscribeMessage
}